package com.arch.dev.server.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;

import com.arch.dev.server.report.ReportFactory;

/**
 * 
 * Servlet implementation class PdfServlet
 */

public class ExcelServiceServlet extends HttpServlet {
	
	Logger logger = Logger.getLogger(ExcelServiceServlet.class);	

	private static final long serialVersionUID = 1L;

	/**
	 * 
	 * Default constructor.
	 */

	public ExcelServiceServlet() {

		
	}

	/**
	 * 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// TODO Auto-generated constructor stub
				logger.info("reached excel service");
				
				// Check that we have a file upload request
				boolean isMultipart = ServletFileUpload.isMultipartContent(request);
				
				// Create a factory for disk-based file items
				FileItemFactory factory = new DiskFileItemFactory();

				// Create a new file upload handler
				ServletFileUpload upload = new ServletFileUpload(factory);

				// Parse the request
				List items;
				try {
					items = upload.parseRequest(request);
					// Process the uploaded items
					Iterator iter = items.iterator();
					while (iter.hasNext()) {
					    FileItem item = (FileItem) iter.next();

					    if (!item.isFormField()) {
					        String fieldName = item.getFieldName();
					        String fileName = item.getName();
					        String contentType = item.getContentType();
					        boolean isInMemory = item.isInMemory();
					        long sizeInBytes = item.getSize();
					        logger.info(fieldName + " " +  fileName + " " + sizeInBytes  + " " + isInMemory);
					        
							ServletContext sc = this.getServletContext();

							logger.info("*********" + sc.getRealPath("/"));

							File directory = new File(sc.getRealPath("/") + "Excel");

							if (!directory.exists()) {

								directory.mkdir();

							}
							
					        File uploadedFile = new File(directory, fileName);
					        try {
								item.write(uploadedFile);
								ServletOutputStream out  = response.getOutputStream();
								response.setContentType("text/html");
								out.print(uploadedFile.getAbsolutePath());
								out.close();
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
					    }
					}
				} catch (FileUploadException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			

	}

	/**
	 * 
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
